<template>
  <div class="app-container">
    <el-form class="formWidth" :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="用工单位名称" prop="companyName">
        <el-input
          v-model="queryParams.companyName"
          placeholder="请输入用工单位名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所在省" prop="provinceId">
        <el-select v-model="queryParams.provinceId" placeholder="请选择所在省" clearable size="small" @change="getSearchCity">
          <el-option v-for="(item,index) in provinceArr" :key="index" :label="item.name" :value="item.code" />
        </el-select>
      </el-form-item>
      <el-form-item label="所在城市" prop="cityId">
        <el-select v-model="queryParams.cityId" placeholder="请选择所在城市" clearable size="small">
          <el-option v-for="(item,index) in searchCityArr" :key="index" :label="item.name" :value="item.code" />
        </el-select>
      </el-form-item>
      <el-form-item label="联系人" prop="contact">
        <el-input
          v-model="queryParams.contact"
          placeholder="请输入联系人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系人手机号" prop="contactPhoneNo">
        <el-input
          v-model="queryParams.contactPhoneNo"
          placeholder="请输入联系人手机号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="公司地址" prop="address">
        <el-input
          v-model="queryParams.address"
          placeholder="请输入公司地址"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="公司类型" prop="companyType">
        <el-select v-model="queryParams.companyType" placeholder="请选择公司类型" clearable size="small">
          <el-option
            v-for="dict in companyTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="公司规模" prop="companyScale">
        <el-select v-model="queryParams.companyScale" placeholder="请选择公司规模" clearable size="small">
          <el-option
            v-for="dict in companyScaleOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="吸纳就业人数" prop="receivedSoldierCnt">
        <el-input
          v-model="queryParams.receivedSoldierCnt"
          placeholder="请输入吸纳就业人数"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="公司星级" prop="starLevel">
        <el-select v-model="queryParams.starLevel" placeholder="请选择公司星级" clearable size="small">
          <el-option
            v-for="dict in starLevelOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item> -->
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['jobservice:company:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['jobservice:company:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['jobservice:company:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['jobservice:company:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="companyList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="用工单位ID" align="center" prop="companyId" /> -->
      <el-table-column label="用工单位名称"  show-overflow-tooltip align="center" prop="companyName" />
      <el-table-column label="所在省" show-overflow-tooltip align="center" width="90px" prop="provinceName" />
      <el-table-column label="所在城市" show-overflow-tooltip align="center" prop="cityName" />
      <el-table-column label="联系人" show-overflow-tooltip align="center" prop="contact" />
      <el-table-column label="联系人手机号" show-overflow-tooltip align="center" prop="contactPhoneNo" />
      <el-table-column label="公司地址" show-overflow-tooltip align="center" prop="address" />
      <el-table-column label="公司类型" show-overflow-tooltip align="center" prop="companyType" :formatter="companyTypeFormat" />
      <!-- <el-table-column label="公司规模" width="100px" show-overflow-tooltip align="center" prop="companyScale" :formatter="companyScaleFormat" /> -->
      <!-- <el-table-column label="公司描述" show-overflow-tooltip align="center" prop="companyDesc"></el-table-column> -->
      <!-- <el-table-column label="吸纳就业人数" width="100px" show-overflow-tooltip align="center" prop="receivedSoldierCnt" /> -->
      <!-- <el-table-column label="公司星级" width="80px" show-overflow-tooltip align="center" prop="starLevel" :formatter="starLevelFormat" /> -->
      <el-table-column label="状态" width="80px" show-overflow-tooltip align="center" prop="status" :formatter="statusFormat" />
      <el-table-column label="操作" width="200px" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['jobservice:company:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['jobservice:company:remove']"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-user"
            @click="openAccount(scope.row)"
            v-hasPermi="['jobservice:company:account']"
          >开通账号</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改用工单位信息对话框 -->
    <el-dialog class="addDia" :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form class="formWidth" ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="用工单位名称" prop="companyName">
              <el-input v-model="form.companyName" placeholder="请输入用工单位名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="公司地址" prop="address">
              <el-input v-model="form.address" placeholder="请输入公司地址" />
            </el-form-item>
          </el-col>
         <el-col :span="12">
            <el-form-item label="所在省" prop="provinceId">
              <el-select v-model="form.provinceId" placeholder="请选择所在省" @change="getCity" clearable>
                 <el-option v-for="(item,index) in provinceArr" :key="index" :label="item.name" :value="item.code" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所在城市" prop="cityId">
              <el-select v-model="form.cityId" placeholder="请选择所在城市" clearable>
                <el-option v-for="(item,index) in cityArr" :key="index" :label="item.name" :value="item.code" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系人" prop="contact">
          <el-input v-model="form.contact" placeholder="请输入联系人" />
        </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系人手机号" prop="contactPhoneNo">
              <el-input v-model="form.contactPhoneNo" placeholder="请输入联系人手机号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="公司类型" prop="companyType">
              <el-select v-model="form.companyType" placeholder="请选择公司类型">
                <el-option
                  v-for="dict in companyTypeOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="parseInt(dict.dictValue)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="公司规模" prop="companyScale">
              <el-select v-model="form.companyScale" placeholder="请选择公司规模" clearable size="small">
                <el-option
                  v-for="dict in companyScaleOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="吸纳就业人数" prop="receivedSoldierCnt">
              <el-input v-model="form.receivedSoldierCnt" placeholder="请输入吸纳就业人数" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="公司星级" prop="starLevel">
              <el-select v-model="form.starLevel" placeholder="请选择公司星级">
                <el-option
                  v-for="dict in starLevelOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="parseInt(dict.dictValue)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="公司描述" prop="companyDesc">
              <el-input v-model="form.companyDesc" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态" prop="status">
              <el-select v-model="form.status" placeholder="请选择状态">
                <el-option
                  v-for="dict in statusOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="parseInt(dict.dictValue)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 开通账户 -->
    <el-dialog title="开通账户" :visible.sync="accountDia" width="450px" append-to-body>
      <el-form class="formWidth" ref="accountForm" :model="accountForm" :rules="accountRules" label-width="100px">
        <el-form-item label="用户名" prop="userName">
          <el-input v-model="accountForm.userName" placeholder="请输入用户名"  />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="accountForm.password" placeholder="请输入密码" type="password" />
        </el-form-item>
        <el-form-item label="部门" prop="deptName">
          <el-input v-model="accountForm.deptName" readonly placeholder="部门" />
        </el-form-item>
        <el-form-item label="角色" prop="roleName">
          <el-input v-model="accountForm.roleName" readonly placeholder="请输入公司地址" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitAccount">确 定</el-button>
        <el-button @click="cancelAccount">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { listCompany, getCompany, delCompany, addCompany, updateCompany, exportCompany, provinceList, cityList, openAcc } from "@/api/jobservice/company";

export default {
  name: "Company",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用工单位信息表格数据
      companyList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 公司类型字典
      companyTypeOptions: [],
      // 公司规模字典
      companyScaleOptions: [],
      // 公司星级字典
      starLevelOptions: [],
      // 状态字典
      statusOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        companyName: null,
        provinceId: null,
        cityId: null,
        contact: null,
        contactPhoneNo: null,
        address: null,
        companyType: null,
        companyScale: null,
        companyDesc: null,
        receivedSoldierCnt: null,
        starLevel: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        companyName: [
          { required: true, message: "用工单位名称不能为空", trigger: "blur" }
        ],
        provinceId: [
          { required: true, message: "所在省不能为空", trigger: "change" }
        ],
        cityId: [
          { required: true, message: "所在城市不能为空", trigger: "change" }
        ],
        contact: [
          { required: true, message: "联系人不能为空", trigger: "blur" }
        ],
        contactPhoneNo: [
          { required: true, message: "联系人手机号不能为空", trigger: "blur" }
        ],
        address: [
          { required: true, message: "公司地址不能为空", trigger: "blur" }
        ],
        companyType: [
          { required: true, message: "公司类型不能为空", trigger: "change" }
        ],
        companyScale: [
          { required: true, message: "公司规模不能为空", trigger: "blur" }
        ],
        companyDesc: [
          { required: true, message: "公司描述不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "状态不能为空", trigger: "change" }
        ],
      },
      provinceArr: [], //省
      cityArr: [], //新增修改城市
      searchCityArr: [], //查询条件城市
      accountDia: false, // 开通账户弹框
      accountForm: {
        userName: '',
        password: '',
        deptId: 100,
        deptName: '军人退役管理平台',
        roleId: 3,
        roleName: '企业用户角色',
        companyId: null,
      },
      accountRules: {
        userName:[ { required: true, message: "用户名不能为空", trigger: "change" }],
        password:[ { required: true, message: "密码不能为空", trigger: "change" }],
        deptName:[ { required: true, message: "部门不能为空", trigger: "change" }],
        roleName:[ { required: true, message: "角色不能为空", trigger: "change" }]
      },

    };
  },
  created() {
    this.getList();
    this.getDicts("s_company_type").then(response => {
      this.companyTypeOptions = response.data;
    });
    this.getDicts("s_company_scale").then(response => {
      this.companyScaleOptions = response.data;
    });
    this.getDicts("s_company_level").then(response => {
      this.starLevelOptions = response.data;
    });
    this.getDicts("s_company_status").then(response => {
      this.statusOptions = response.data;
    });
    this.getProvince();
  },
  methods: {
    /** 查询用工单位信息列表 */
    getList() {
      this.loading = true;
      listCompany(this.queryParams).then(response => {
        this.companyList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 公司类型字典翻译
    companyTypeFormat(row, column) {
      return this.selectDictLabel(this.companyTypeOptions, row.companyType);
    },
    // 公司规模字典翻译
    companyScaleFormat(row, column) {
      return this.selectDictLabel(this.companyScaleOptions, row.companyScale);
    },
    // 公司星级字典翻译
    starLevelFormat(row, column) {
      return this.selectDictLabel(this.starLevelOptions, row.starLevel);
    },
    // 状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 获取省
    getProvince(){
      provinceList().then(response => {
        this.provinceArr = response.data;
      })
    },
    // 获取查询条件中的市数组
    getSearchCity(value){
      this.queryParams.cityId = null;
      if(value){
        cityList(value).then(response => {
          this.searchCityArr = response.data
        })
      }else{
         this.searchCityArr = [];
      }
    },
    // 获取新增和修改市
    getCity(value){
      this.form.cityId = null;
      if(value){
        cityList(value).then(response => {
          this.cityArr = response.data
        })
      }else{
         this.cityArr = [];
      }
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        companyId: null,
        companyName: null,
        provinceId: null,
        cityId: null,
        contact: null,
        contactPhoneNo: null,
        address: null,
        companyType: null,
        companyScale: null,
        companyDesc: null,
        receivedSoldierCnt: null,
        starLevel: null,
        status: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.companyId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.cityArr = [];
      this.open = true;
      this.title = "添加用工单位信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.getCity(row.provinceId); // 获取城市列表
      const companyId = row.companyId || this.ids
      getCompany(companyId).then(response => {
        this.form = response.data;
        this.form.companyScale = String(this.form.companyScale)
        this.open = true;
        this.title = "修改用工单位信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.companyId != null) {
            updateCompany(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCompany(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const companyIds = row.companyId || this.ids;
      // this.$confirm('是否确认删除用工单位信息编号为"' + companyIds + '"的数据项?', "警告", {
        this.$confirm('是否确认删除该项数据?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delCompany(companyIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有用工单位信息数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportCompany(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    },

    // 开通账号
    openAccount(row){
      this.resetAccount();
      this.accountDia = true;
    },
     // 表单重置
    resetAccount() {
      this.accountForm = {
        userName: '',
        password: '',
        deptId: 100,
        deptName: '军人退役管理平台',
        roleId: 3,
        roleName: '企业用户角色',
        companyId: null,
      }
      this.resetForm("accountForm");
    },
    // 开通账户提交
    submitAccount(){
     this.$refs["accountForm"].validate(valid => {
        if (valid) {
          openAcc(this.accountForm).then(response => {
            this.msgSuccess("开通成功");
            this.accountDia = false;
          })
        }
     })
    },
    cancelAccount(){
      this.accountDia = false;
      this.resetAccount();
    }
  }
};
</script>
<style scoped>
.formWidth .el-textarea{
  width: 220px !important;
}
</style>
